﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AMAP.Areas.Admin.Models.Account;
using System.Web.Security;
using AMAP.ServiceInterfaces;
using AMAP.Domain;

namespace AMAP.Areas.Admin.Controllers
{
    public class AccountController : Controller
    {
        private IVolunteerService VolunteerService;
        private IRoleService RoleService;
        private IAmapService AmapService;
        private IVolunteerRoleService VolunteerRoleService;
        private IUserService UserService;
        private ICoordinatesService CoordinatesService;

        public AccountController(IVolunteerService VolunteerService, IRoleService RoleService, IAmapService AmapService, IVolunteerRoleService VolunteerRoleService, IUserService UserService, ICoordinatesService CoordinatesService)
        {
            this.VolunteerService = VolunteerService;
            this.RoleService = RoleService;
            this.AmapService = AmapService;
            this.VolunteerRoleService = VolunteerRoleService;
            this.UserService = UserService;
            this.CoordinatesService = CoordinatesService;

        }

        //
        // GET: /Admin/Account/

        public ActionResult Index()
        {
            return View();
        }

        [Authorize(Roles="President")]
        public ActionResult Create()
        {
            AdminAccountCreateViewModel Model = new AdminAccountCreateViewModel();
            Model.RoleList = RoleService.GetAllAsList();
            Model.AmapList = new SelectList(AmapService.GetAllAsList(), "Id", "Name");
            return View(Model);
        }

        [HttpPost]
        [Authorize(Roles = "President")]
        public ActionResult Create(AdminAccountCreateViewModel Model)
        {
            if (ModelState.IsValid)
            {
                Domain.Volunteer Volunteer = VolunteerService.Create();

                AutoMapper.Mapper.Map(Model, Volunteer);
                AutoMapper.Mapper.Map(Model, Volunteer.User);
                Volunteer.Amap = AmapService.GetApplicationAMAP();

                VolunteerService.Add(Volunteer);

                //addWithoutSaving(Objet)
                foreach (String id in Model.Roles)
                {
                    this.VolunteerRoleService.AddWithoutSaving(new VolunteerRole { RoleId = Convert.ToInt32(id), VolunteerId = Volunteer.Id });
                }

                this.VolunteerRoleService.Save();

                //Connect the registrated user
                FormsAuthentication.SetAuthCookie(Model.Email, false);

                return RedirectToRoute("Admin_default", new { Controller = "Home", action = "Index" });
            }
            else
            {
                Model.RoleList = RoleService.GetAllAsList();
            }

            return View(Model);
        }

        [Authorize(Roles = "President")]
        public ActionResult AddCoordinates()
        {
            AdminAccountAddCoordinatesViewModel Model = new AdminAccountAddCoordinatesViewModel();
            return View(Model);
        }

        [HttpPost]
        [Authorize(Roles = "President")]
        public ActionResult AddCoordinates(AdminAccountAddCoordinatesViewModel Model)
        {
            Domain.User Volunteer = UserService.GetByEmail(User.Identity.Name);
            Volunteer.Coordinates = new Domain.Coordinates();

            AutoMapper.Mapper.Map(Model, Volunteer.Coordinates);
            Volunteer.Coordinates = GeoCoder.SetGPSCoordinates(Volunteer.Coordinates);
            UserService.Save();

            return RedirectToRoute("Admin_default", new { controller = "Home", action = "Index" });
        }

        [Authorize(Roles = "President")]
        public ActionResult EditCoordinates()
        {
            AdminAccountEditCoordinatesViewModel Model = new AdminAccountEditCoordinatesViewModel();
            Domain.User Volunteer = UserService.GetByEmail(User.Identity.Name);
            AutoMapper.Mapper.Map(Volunteer.Coordinates, Model);
            return View(Model);
        }

        [HttpPost]
        [Authorize(Roles = "President")]
        public ActionResult EditCoordinates(AdminAccountEditCoordinatesViewModel Model)
        {
            Domain.Coordinates Coordinates = this.UserService.GetByEmail(User.Identity.Name).Coordinates;

            //Due to EF4 issues, we cannot automap ViewModel content into the Coordinates POCO
            //(which isn't really a POCO in this case but a EF Proxy object)

            Coordinates.Address1 = Model.Address1;
            Coordinates.Address2 = Model.Address2;
            Coordinates.City = Model.City;
            Coordinates.PhoneNumber = Model.PhoneNumber;
            Coordinates.PostalCode = Model.PostalCode;
            Coordinates = GeoCoder.SetGPSCoordinates(Coordinates);
            this.CoordinatesService.Update(Coordinates);

            return RedirectToRoute("Admin_default", new { controller = "Home", action = "Index" });
        }

    }
}
